﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;
using Telerik.Windows;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;
using HeThongThongTin.Library;
using Converter;
using System.Resources;
using System.Reflection;


namespace HeThongThongTin.UI.SL.DanhMuc.DM_GiayTo
{
    public partial class DM_GiayTo_ListView : UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            // no parameters
        }

        public string Title
        {
            get { return "GiayTo"; }
        }

        #endregion

        #region Load Methods
        public DM_GiayTo_ListView()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("HeThongThongTin.UI.SL.DanhMuc.DM_GiayTo.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.DM_GiayTo_GeneralInformation_GroupBox, controls);
            this.DM_GiayTo_GridView.AddHandler(GridViewCellBase.CellDoubleClickEvent, new EventHandler<RadRoutedEventArgs>(OnCellDoubleClick), true);             
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                System.Windows.Data.CollectionViewSource dm_GiayTo_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_GiayTo_ListViewModelSource"];
                DM_GiayTo_ListViewModel dm_GiayTo_ListViewModel = new DM_GiayTo_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
                dm_GiayTo_collectionViewSource.Source = new List<object> { dm_GiayTo_ListViewModel };

                
                System.Windows.Data.CollectionViewSource dm_GiayTo_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_GiayTo_ViewModelSource"];
                DM_GiayTo_ViewModel dm_GiayTo_ViewModel = new DM_GiayTo_ViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
                dm_GiayTo_ViewSource.Source = new List<object> { dm_GiayTo_ViewModel };
                dm_GiayTo_ViewModel.Saved += new EventHandler(dm_GiayTo_ViewModel_Saved);

              
            }
        }       
        #endregion

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {

            ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
        }


        #region View Model Related Methods

        void dm_GiayTo_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_GiayTo_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_GiayTo_ListViewModelSource"];
            DM_GiayTo_ListViewModel vm = ((List<object>)dm_GiayTo_ListViewSource.Source)[0] as DM_GiayTo_ListViewModel;
            vm.GetDM_GiayTo_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
            
        }

        private void OnCellDoubleClick(object sender, RadRoutedEventArgs args)
        {
            if (this.DM_GiayTo_GridView.SelectedItem != null)
            {
                System.Windows.Data.CollectionViewSource dm_GiayTo_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_GiayTo_ViewModelSource"];
                DM_GiayTo_ViewModel vm = ((List<object>)dm_GiayTo_ViewSource.Source)[0] as DM_GiayTo_ViewModel;
                int parameter = ((DM_GiayToInfo)this.DM_GiayTo_GridView.SelectedItem).Khoa;
                Csla.Xaml.ExecuteEventArgs eventObject = new Csla.Xaml.ExecuteEventArgs();
                eventObject.MethodParameter = parameter;
                vm.GetDM_GiayTo(this, eventObject);
            }
        }
        #endregion

    }
}
